package org.example.com.service.impl;

import org.example.com.mapper.FavoriteMapper;
import org.example.com.mapper.HouseTagsMapper;
import org.example.com.pojo.HouseTag;
import org.example.com.service.FavoriteService;
import org.example.com.pojo.vo.FavoriteVO;
import org.example.com.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private HouseTagsMapper houseTagsMapper;

    /**
     * 获取收藏的房源
     * @return
     */
    @Override
    public List<FavoriteVO> getList() {
        //获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        System.out.println(claims);
        int userId = (int) claims.get("id");

        List<FavoriteVO> list = favoriteMapper.getList(userId);
//        for (FavoriteVO favoriteVO : list) {
//            List<HouseTag> tagsList = houseTagsMapper.getListByHouseId(favoriteVO.getId());
//            favoriteVO.setTags(tagsList);
//        }

        return list;
    }

    /**
     * 添加收藏
     * @param houseId
     */
    @Override
    public void addFavorite(int houseId) {
        //获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        System.out.println(claims);
        int userId = (int) claims.get("id");

        favoriteMapper.add(userId, houseId);

    }

    /**
     * 删除收藏
     * @param houseId
     */
    @Override
    public void deleteFavorite(int houseId) {
        //获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        System.out.println(claims);
        int userId = (int) claims.get("id");

        favoriteMapper.delete(userId, houseId);
    }
}
